﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Text;
using System.Data;
using PIM_ATE.Electrical;
using PIM_ATE.Mods;
using System.Reflection;

namespace PIM_ATE
{
    static class Program
    {
        private static Mutex FapiTaskMutex;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            if (!GDefine.IS_LOCAL)
            {

                #region 调用web接口验证软件版本有效性
                try
                {
                    PIM_ATE.Mods.ClsDataResultForMOD csVersion = new Mods.ClsDataResultForMOD();
                    Assembly asm = Assembly.GetExecutingAssembly();
                    var asmName = asm.GetName();
                    if (!GDefine.IS_VERSION_NOCHECK && !csVersion.IsValid_PIM_ZiGuang_ClientSoft(asmName.Version.ToString().Substring(0, 3)))
                    //if (asmName.Version.ToString() != "1.0.88.0")
                    {
                        MessageBox.Show("软件版本(" + asmName.Version.ToString().Substring(0, 3) + ")无效, 请更新");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("软件版本验证失败: " + ex.Message);
                    return;
                }
                #endregion

                #region MUTEX
                try
                {//防止同时打开好几个应用程序
                    FapiTaskMutex = Mutex.OpenExisting(@"Global\FapiTaskMutex");
                    MessageBox.Show("程序已运行！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }
                catch (WaitHandleCannotBeOpenedException)
                {
                    FapiTaskMutex = new Mutex(false, @"Global\FapiTaskMutex");
                }
                catch (Exception)
                {
                    return;
                }
                #endregion
            }


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            #region DataBase

            string sConnect = "";
            string sPath, sDbName, sUserID, sPassword, sUrl;

            #region DEBUG

            sPath = "192.168.0.3,1433";
            sDbName = "jcdb";
            sUserID = "jc#15User";
            sPassword = "No19@Data";

            sConnect = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
                                     sPath, sDbName, sUserID, sPassword);
            MicroDBHelper.SetConnection(sConnect, "DEBUG");

            sPath = "122.224.77.122,2347";
            sDbName = "jcdb";
            sUserID = "jc#15User";
            sPassword = "No19@Data";

            sConnect = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
                                     sPath, sDbName, sUserID, sPassword);
            MicroDBHelper.SetConnection(sConnect, "ATE");

            #endregion

            #region ANTENNA

            IniFile.SetFileName(System.Windows.Forms.Application.StartupPath + "\\JcOther.ini");

            sPath = IniFile.IniReadValue("ANTENNA_Database", "DBPath", "10.53.20.172,1433");
            sDbName = IniFile.IniReadValue("ANTENNA_Database", "DBName", "ATS");
            sUserID = IniFile.IniReadValue("ANTENNA_Database", "User", "ZGJTRJ");
            sPassword = IniFile.IniReadValue("ANTENNA_Database", "Password", "20170309");

            if (GDefine.IS_MY_DB)
            {
                sPath = "10.53.28.170,1433";
                sDbName = "ATS";
                //sUserID = "sa";
                //sPassword = "mobi@20120510";
                sUserID = "sa";
                sPassword = "123";
            }
            if (GDefine.IS_LOCAL)
            {
                sPath = ".,1433";
                sDbName = "ATS";
                //sUserID = "sa";
                //sPassword = "mobi@20120510";
                //sUserID = "sa";
                //sPassword = "123";
            }
         
            sConnect = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
                                     sPath, sDbName, sUserID, sPassword);
            MicroDBHelper.SetConnection(sConnect, "ANTENNA");

            #endregion

            #region MOD

            IniFile.SetFileName(System.Windows.Forms.Application.StartupPath + "\\JcOther.ini");

            sPath = IniFile.IniReadValue("RF_Database", "DBPath", "10.53.20.172,1433");
            sDbName = IniFile.IniReadValue("RF_Database", "DBName", "RF_PIMTestConfig");
            sUserID = IniFile.IniReadValue("RF_Database", "User", "ZGJTRJ");
            sPassword = IniFile.IniReadValue("RF_Database", "Password", "20170309");
            sUrl = IniFile.IniReadValue("RF_Database", "URL", "http://10.53.20.170/pa/WebService/ServiceForVendor/PIM_TestAndDataUpload_Service.asmx");
            ClsLocalRecord.sCopyPath = IniFile.IniReadValue("RF_Database", "CopyPath", "D:\\PIM-ATE-Report");
            ClsLocalRecord.sCopyPathSecondTest = IniFile.IniReadValue("RF_Database", "CopyPath", "D:\\交调复测数据");
               
            //sPath = "10.53.20.172,1433";
            //sDbName = "RF_PIMTestConfig";
            //sUserID = "ZGJTRJ";
            //sPassword = "20170309";

            sConnect = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
                                     sPath, sDbName, sUserID, sPassword);
            MicroDBHelper.SetConnection(sConnect, "MOD");
            ClsDataResultForMOD.url = sUrl;

            #endregion
            //throw new ArgumentException("The argument 'num' is less than zero!");
            #endregion

            #region Form
            ClsDataUser userObj = new ClsDataUser();
            if (GDefine.RUNNING_MODE == 0)
            {
                userObj.sEquID = PIMFunction.GetIPAddress();
                FrmUser frmlgn = new FrmUser(userObj);
                if (frmlgn.ShowDialog() == DialogResult.OK)
                {
                    userObj.sDarkroomNum = frmlgn.textBoxDarkroomNum.Text;
                    if (frmlgn.cbTestItem.SelectedIndex == 0)
                    {
                        Application.Run(new FrmMain(userObj));
                    }
                    else if (frmlgn.cbTestItem.SelectedIndex == 1)
                    {
                        Application.Run(new FrmSParam(userObj));
                    }
                    else
                    {
                        Application.Run(new FrmConfigSelect(userObj));
                    }
                }
            }
            else
            {
                userObj.sEquID = PIMFunction.GetIPAddress();
                FrmLogin frmlgn = new FrmLogin(userObj);
                frmlgn.ControlBox = true;
                if (frmlgn.ShowDialog() == DialogResult.OK)
                {
                    //userObj.sUser = frmlgn.textBoxUser.Text;
                    //userObj.sDarkroomNum = frmlgn.textBoxDarkroomNum.Text;
                    Application.Run(new FrmMods(userObj));
                }
            }
            #endregion
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Insert_Standard_ErrorLog.WriteException(e.Exception);
            Application.Exit();
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            Insert_Standard_ErrorLog.WriteException(ex);
            //MiniDumpUtil.TryWriteMiniDump("C:\\Users\\san\\Desktop\\gdb-remote-bin\\Dump\\test.dmp",
            //                              MiniDumpType.MiniDumpWithFullMemory);
            Application.Exit();
        }
    }
}
